package com.synjones.handsetS8;

import android.content.Context;
import com.synjones.idcard.IDCardReaderRetInfo;
import com.synjones.idcard.IDcardReader;
import com.synjones.multireaderlib.MultiReader;

import java.io.File;
import java.io.IOException;

import android_serialport_api.SerialPort;

/**
 * 二代证模块操作封装
* @ClassName IdCardReaderModule 
* @author zhaodianbo@Synjones
* @date 2015-7-22 下午3:56:44 
*
 */
public class IDCardReaderModule {
	/**
	 * 二代证命令封装
	 */
	MultiReader multiReader=MultiReader.getReader();
	/**
	 * 二代证读卡器对象
	 */
	IDcardReader idCardReader;
	Context mContext;
	SerialPort serialPort;
	DataTransChannel dataTransChannel;
	boolean isOpen=false;
	static IDCardReaderModule idCardReaderModule;
	
	private IDCardReaderModule(Context context){
		mContext=context;
		dataTransChannel=new DataTransChannel();
		// readerDriver.setMaxRFByte((byte) 0x50);
	}

	public static IDCardReaderModule getInstance(Context context){
		if(idCardReaderModule==null){
			idCardReaderModule=new IDCardReaderModule(context);
		}
		return idCardReaderModule;
	}

	public void open(){

		if(serialPort!=null)
			serialPort.close();
		//S8PowerControl.getInstance().setIDReaderPowerOn();
		WtWdPowerUtils.setIDPower(mContext);
		//MyUtils.sleep(500);//使模块上电完成
		try {
			serialPort = new SerialPort(new File("/dev/ttyMT0"), 115200, 0);
			dataTransChannel.setInOutStream(serialPort.getInputStream(),serialPort.getOutputStream());
			dataTransChannel.open();
			isOpen=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		multiReader.setDataTransInterface(dataTransChannel);
		idCardReader=new IDcardReader(multiReader);
		idCardReader.open(mContext);
	}

	public boolean isOpen(){
		return isOpen;
	}
	
	
	/** 结束读卡  关闭读卡器
	* @Title: CloseSerialPort    
	* @return void 
	*/
	public void close(){	
		try {
			idCardReader.close();
		} catch (Exception e) {}
		if(serialPort!=null) {
			try {
				serialPort.close();
			}catch (Exception e){}
		}
		isOpen = false;
		if(dataTransChannel!=null){
			try{
				dataTransChannel.close();
			}catch (Exception e){}
		}
		//S8PowerControl.getInstance().setIDReaderPowerOff();
		WtWdPowerUtils.closeIDPower(mContext);
	}
	
	
	/**  获取身份证信息，此函数将阻塞直到返回身份证或读卡超时
	* @Title: getIDcardBlocking   
	* @return IDCard 
	*/
	public IDCardReaderRetInfo getIDcardBlockingNoFp(boolean readOnce, boolean needDecodePhoto){
		return idCardReader.getIDcardBlockingNoFpRetInfo(readOnce, needDecodePhoto);
	}
	
	/**
	 * 获取身份证信息
	* @Title getIDcardInfo 
	* @param sameCardReadOnce 设置为true时，卡不移开，读同一张卡时，只有第一次返回信息，后续都返回寻卡错误。
	* @param needPhoto 是否需要照片信息
	* @param needFp 是否需要指纹信息
	* @return
	 */
	public IDCardReaderRetInfo getIDcardInfo(boolean sameCardReadOnce, boolean needPhoto, boolean needFp){
		return idCardReader.getIDCardInfo(sameCardReadOnce, needPhoto, needFp);
	}
	/**
	 * 获取身份证信息
	* @Title getIDcardInfo 
	* @return
	 */
	public IDCardReaderRetInfo getIDcardInfo(){
		return idCardReader.getIDCardInfo(false, true, false);
	}
	
	/**
	 * 获取身份证安全模块编号
	* @Title getSamvIDString 
	* @return
	 */
	public String getSamvIDString(){
		return idCardReader.getSamvIDStr();
	}
	/**
	 * 获取身份证中追加地址信息
	* @Title getAppendInfo 
	* @return
	 */
	public String getAppendAddress(){
		return idCardReader.readAppendAddress();
	}
	
	/**
	 * 获取二代证读卡器对象
	* @Title getIDcardReader 
	* @return
	 */
	public IDcardReader getIDcardReader(){
		return idCardReader;
	}

	public MultiReader getBaseMultiReader() {
		return multiReader;
	}
}
